The PrintForm method is by far the simplest way to print. Simply invoke a form object's PrintForm method, and the form sends an image of itself including an controls it contains to the printer. If the form's AutoRedraw property is set to true, the printout also includes any graphics you have drawn on the form. While this method is extremely simple, it has a couple of drawbacks.
First, PrintForm can only display graphics drawn on the form if AutoRedraw is true. This means your program must use more memory than it might otherwise need is you want to print those graphics.
Second, the image is a bit-by-bit copy of whatever the form looks like on your screen. Your monitor probably has a much lower resolution than your printer does. A typical computer monitor has a resolution of 96 pixels per inch. Many printers have resolutions of 300 dots per inch. When the 96 pixels per inch picture is stretched to a reasonable size at 300 dots per inch, it appears rough and grainy.
Another problem with the PrintForm method is that it usually sends far more data to the printer than necessary. Suppose you have a 400-by-400 pixel form with the word "Hello" displayed in the middle. The PrintForm method sends your printer information describing all of the form's 160,000 pixels even though only a few contain interesting data. Sending the printer only the text "Hello," together with some information about the text's position and font characteristics, is much faster.
Some applications have more subtle problems with PrintForm. If the form contains a scrolling region, like a text box with scrollbars, PrintForm displays only the portion of the region that is visible at the time it is invoked. If the user resizes the form or if there is an unknown amount of information in the scrolling region, it may be difficult to tell if all of the information is visible.
Despite these drawbacks, the PrintForm method is simple and powerful. With a single line of code, you can print almost anything. For that reason it is still useful, particularly in the early stages of a programming project. In the project's later stages, when form layouts are final, you can always go back and write printing routines that take advantage of the printer's higher resolution.
High-Resolution Printing
Printer objects provide most of the same graphic properties and methods provided by forms and picture boxes. Printer objects have the properties CurrentX, CurrentY, DrawMode, DrawStyle, DrawWidth, FillColor, FillStyle, Font, FontBold, FontCount, FontItalic, FontName, FontSize, FontStrikethru, FontTransparent, FontUnderline, ForeColor, ScaleHeight, ScaleLeft, ScaleMode, ScaleTop, ScaleWidth, TwipsPerPixelX, and TwipsPerPixelY. Printer objects also provide the methods Circle, Line, PaintPicture, Print, PSet, Scale, ScaleX, ScaleY, TextHeight, and TextWidth.
Printer objects even have device contexts and support API drawing functions such as Polygon and Polyline. However, the API functions print nothing unless your program uses some Visual Basic method to print something first. If you do not want the Visual Basic method to modify the printout, use the following statement:
Printer.Print ""
Although this statement does nothing visible, it initializes the printer so the API functions can work properly.
Example program PolyPrnt uses the Polyline, Polygon, PolyPolyline, and PolyPolygon API functions to draw print several shapes. It uses Visual Basic's Line method to draw four boxes on the printout so it does not need to print a blank string to initialize the printer.
Using Visual Basic and API drawing methods, you can print anything you can display on a form or picture box at the printer's full resolution. To produce high-quality output, however, there are still a couple of details you must be prepared to handle.
Printing Text
Using the Printer object's CurrentX and CurrentY properties, and its Print method, you can print text. The Print command knows nothing about margins, however. If you print a very long string using the Print statement, the text will extend off the right side of the printed page and be invisible. If you want text to wrap when it reaches the printer's right margin, you must write code to control the positioning of each word.
The simplest way to wrap text is to examine the string one word at a time. For each word, use the Printer object's TextWidth function to see how wide the word will be on the printer. If that width added to CurrentX is greater than the right margin, start a new line.
Similarly when the program starts a new line, it should check the Printer object's CurrentY property to see if there is room for the next line of text. If there is no more room on the page, the program can use the NewPage metod to start a new page.
After examining the new word, possibly starting a new line or page, the program should use the Print statement to display the word.
Example program WordWrap uses this technique to print text. The program also supports simple paragraph formatting. When it finds a carriage return and line feed combination, it starts a new paragraph by skipping some extra vertical space and indenting the paragraph's first word.